
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <DHT.h>
#include <WiFi.h>

const char *ssid = "123";//WIFI
const char *password = "1984832368";//WIFI密码
const char* serverIP = "183.230.40.33"; //欲访问的地址
uint16_t serverPort = 80;         //服务器端口号
String url = "http://api.heclouds.com/devices/762634087/datapoints?type=3";//网址 设备ID
String api="h8tpETsrj==4tTdrjbSgYFNsgW4=";//api-key
String post;//http请求
int Content_Length;
WiFiClient client; //声明一个客户端对象，用于与服务器进行连接


U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* cs=*/ 10, /* dc=*/ 18, /* reset=*/ 19);

#define LED_BUILTIN 27
#define GROUND_H    13
#define DHTPIN 23 //DHT11 DATA 数据引脚

#define DHTTYPE DHT11 //选择的类型

DHT dht(DHTPIN, DHTTYPE);

long tank_temp = 0, light_value = 0, ground_h = 0;
float house_h = 0, house_t = 0;
int8_t rx_data = 0, beep_flag = 0, water_ok_flag = 0, water_in_flag = 0, O2_in_flag = 0;
static unsigned long time_=0;//定义初始发送时间为0
static unsigned long time_out=0;//定义超时发送时间为0

void setup(void)
{
    u8g2.begin();
    u8g2.enableUTF8Print();        // enable UTF8 support for the Arduino print() function
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(GROUND_H, INPUT);
    pinMode(GROUND_H, INPUT_PULLUP);
    Serial.begin(115200);
    Serial.println("DHT22 sensor!");
    dht.begin();

    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);//连接到网络

    while (WiFi.status() != WL_CONNECTED) {//等待网络连接成功
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());//打印模块IP
}

void loop(void)
{
     house_h = dht.readHumidity();
     house_t = dht.readTemperature();
     ground_h = digitalRead(GROUND_H);
    // if (isnan(house_h) || isnan(house_t))
    // {
    //     Serial.println("Failed to read from DHT sensor!");
    //     return;
    // }
    // Serial.print("Humidity: ");
    // Serial.print(house_h);
    // Serial.print(" %t");
    // Serial.print("Temperature: ");
    // Serial.print(house_t);
    // Serial.println(" *C ");

    // // turn the LED on (HIGH is the voltage level)
    // digitalWrite(LED_BUILTIN, HIGH);

    // delay(1000);

    // // turn the LED off by making the voltage LOW
    // digitalWrite(LED_BUILTIN, LOW);

    // // wait for a second
     //delay(5000);

    if(!ground_h || rx_data)
        digitalWrite(LED_BUILTIN, LOW);
    else digitalWrite(LED_BUILTIN, HIGH);

    u8g2.setFont(u8g2_font_unifont_t_chinese2);
    u8g2.clearBuffer(); //清除模组的缓存
    u8g2.setFont(u8g2_font_ncenB08_tr); // 设置字体
    u8g2.setCursor(0, 15);
    u8g2.print("TEMP-WATER:");
    u8g2.setCursor(0, 30);
    u8g2.printf("GOUND-H: %d",ground_h);
    u8g2.setCursor(0, 45);
    u8g2.print("HOUSE-T-H:"); 
    u8g2.setCursor(0, 60);
    u8g2.printf("LIGHT-LUX: %d",ground_h);
    u8g2.sendBuffer(); // 将缓存输出到屏幕

    if (!client.connect(serverIP, serverPort)) {//判断是否连接服务器
        Serial.println("connection failed");
        return;
    }else{//连接成功
       //Serial.println("connection succ");
       //Serial.println();
       String data_value="{\"tank_temp\":"+String(tank_temp)+",\"ground_h\":"+String(ground_h)
          +",\"house_h\":"+String(1)+",\"house_t\":"+String(1)
          +",\"light_value\":"+String(light_value)+",\"O2_in_flag\":"+String(O2_in_flag)
          +",\"test\":"+String(beep_flag)+"}\r\n\r\n";
    //  String data_value="{\"tank_temp\":"+String(tank_temp)+",\"ground_h\":"+String(ground_h)
    //         +"}\r\n\r\n";

       Content_Length=data_value.length();
       post=String("POST ")+url+" HTTP/1.1\r\n"+"api-key: "+api+"\r\n"
          +"Host:api.heclouds.com\r\n"+"Connection:close\r\n"+"Content-Length:"
          +String(Content_Length)+"\r\n\r\n"+data_value;
          
        // Serial.println(Content_Length);
        // Serial.print(post);//串口打印HTTP请求
        //client.print(post);//向服务器发送HTTP请求
      }
       while(client.available()) {//判断WIFI是否有数据
        Serial.print(post);
        String line = client.readStringUntil('\r');//读取WIFI数据赋给line
        Serial.print(line);//打印line 
    }
}
